// import { unified } from 'unified';
// import remarkParse from 'remark-parse';
// import remarkStringify from 'remark-stringify';
let unified: typeof import('unified').unified;
let remarkParse: typeof import('remark-parse').default;
let remarkStringify: typeof import('remark-stringify').default;

export async function loadPackages() {
    ({ unified } = await import('unified'));
    ({ default: remarkParse } = await import('remark-parse'));
    ({ default: remarkStringify } = await import('remark-stringify'));
};
class UnifiedChain {
    processor: ReturnType<typeof unified>
    content: string = ''
    type: string = ''
    plugin: any = undefined

    constructor() {
        this.processor = unified()
    }

    getProcessor() { return this.processor; }

    parseMarkdown(content?: string) {
        if(content) this.content = content
        this.type = 'markdown'
        this.processor.use(remarkParse)
        return this
    }

    use(plugin: any, ...settings: any) {
        this.processor.use(plugin, ...settings)
        return this
    }

    process(content: any): Promise<any> {
        return this.processor.process(content)
    }

    toString() {
        if(this.type == 'markdown') {
            this.use(remarkStringify)
            const res = this.processor.processSync(this.content)
            return String(res)
        }
    }
}

export default UnifiedChain
